const { app, ipcMain } = require('electron')


const isMac = process.platform === 'darwin'

const template = [
  // { role: 'appMenu' }
  ...(isMac ? [{
    label: app.name,
    submenu: [
      {
        label:'关于cloud-doc',
        role: 'about' },
      { type: 'separator' },
      {
        label: '设置',
        accelerator: 'Cmd+,',
        click:() => {
          ipcMain.emit('open-setting-window')
        }
      },
      { type: 'separator' },
      {
        label:'服务',
        role: 'services' },
      { type: 'separator' },
      {
        label:'隐藏',
        role: 'hide'
      },
      {
        label:'隐藏其他',
        role: 'hideothers'
      },
      {
        label:'显示全部',
        role: 'unhide'
      },
      { type: 'separator' },
      {
        label:'退出',
        role: 'quit'
      }
    ]
  }] : [
    {
      label: app.name,
      submenu: [{
        label: '设置',
        accelerator: 'Cmd+,',
        click: () => {
          ipcMain.emit('open-setting-window')
        }
      },]
    }
  ]),
  // { role: 'fileMenu' }
  {
    label: '文件',
    submenu: [
      {
        label: '新建文件',
        accelerator:'CmdOrCtrl+N',
        click: (menuItem, browserWindow, event) => {
          browserWindow.webContents.send('create-new-file')
        }
      },

      { type: 'separator' },
      {
        label: '打开',
        accelerator: 'CmdOrCtrl+O',
        click: (menuItem, browserWindow, event) => {
          browserWindow.webContents.send('import-file')
        }
      },
      { type: 'separator' },
      {
        label: '保存文件',
        accelerator: 'CmdOrCtrl+S',
        click: (menuItem, browserWindow, event) => {
          browserWindow.webContents.send('save-edit-file')
        }
      },
      { type: 'separator' },
      {
        label: '搜索',
        accelerator: 'CmdOrCtrl+F',
        click: (menuItem, browserWindow, event) => {
          browserWindow.webContents.send('search-file')
        }
      }
    ]
  },
  // { role: 'editMenu' }
  {
    label: '编辑',
    submenu: [
      { role: 'undo' },
      { role: 'redo' },
      { type: 'separator' },
      { role: 'cut' },
      { role: 'copy' },
      { role: 'paste' },
      ...(isMac ? [
        { role: 'pasteAndMatchStyle' },
        { role: 'delete' },
        { role: 'selectAll' },
        { type: 'separator' },
        {
          label: 'Speech',
          submenu: [
            { role: 'startSpeaking' },
            { role: 'stopSpeaking' }
          ]
        }
      ] : [
        { role: 'delete' },
        { type: 'separator' },
        { role: 'selectAll' }
      ])
    ]
  },
  // { role: 'viewMenu' }
  {
    label: '视图',
    submenu: [
      { role: 'reload' },
      { role: 'forceReload' },
      { role: 'toggleDevTools' },
      { type: 'separator' },
      { role: 'resetZoom' },
      { role: 'zoomIn' },
      { role: 'zoomOut' },
      { type: 'separator' },
      { role: 'togglefullscreen' }
    ]
  },
  // { role: 'windowMenu' }
  {
    label: '窗口',
    submenu: [
      { role: 'minimize' },
      { role: 'zoom' },
      ...(isMac ? [
        { type: 'separator' },
        { role: 'front' },
        { type: 'separator' },
        { role: 'window' }
      ] : [
        { role: 'close' }
      ])
    ]
  },
  {
    role: 'help',
    submenu: [
      {
        label: '帮助',
        click: async () => {
          const { shell } = require('electron')
          await shell.openExternal('https://electronjs.org')
        }
      }
    ]
  }
]

module.exports = template




